package o;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.os.Handler;
import androidx.annotation.CallSuper;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.audio.AudioProcessor;
import com.google.android.exoplayer2.audio.AudioSink;
import com.google.android.exoplayer2.audio.AudioSink.InitializationException;
import com.google.android.exoplayer2.audio.AudioSink.WriteException;
import com.google.android.exoplayer2.audio.DefaultAudioSink;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.drm.DrmInitData;
import com.google.android.exoplayer2.mediacodec.MediaCodecRenderer;
import com.google.android.exoplayer2.mediacodec.MediaCodecUtil.DecoderQueryException;
import com.huawei.appgallery.festivalanimation.particle.modifiers.SizeModifier;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
import o.acf.d;

@TargetApi(16)
public class aco extends MediaCodecRenderer implements ana {
    private MediaFormat ʻ;
    private boolean ʻॱ;
    private int ʼ;
    private boolean ʽ;
    private int ˊॱ;
    private final Context ˋ;
    private int ˋॱ;
    private final d ˎ;
    private final long[] ˏ;
    private long ˏॱ;
    private int ͺ;
    private final AudioSink ॱ;
    private int ॱˊ;
    private long ॱˋ;
    private boolean ॱॱ;
    private int ॱᐝ;
    private boolean ᐝ;
    private boolean ᐝॱ;

    final class b implements AudioSink.d {
        final /* synthetic */ aco ˎ;

        private b(aco o_aco) {
            this.ˎ = o_aco;
        }

        public void ˊ(int i) {
            this.ˎ.ˎ.ॱ(i);
            this.ˎ.ˋ(i);
        }

        public void ॱ() {
            this.ˎ.ʿ();
            this.ˎ.ᐝॱ = true;
        }

        public void ॱ(int i, long j, long j2) {
            this.ˎ.ˎ.ˎ(i, j, j2);
            this.ˎ.ॱ(i, j, j2);
        }
    }

    public aco(Context context, ahb o_ahb, @Nullable adf<adn> o_adf_o_adn, boolean z, @Nullable Handler handler, @Nullable acf o_acf, @Nullable abz o_abz, AudioProcessor... audioProcessorArr) {
        this(context, o_ahb, o_adf_o_adn, z, handler, o_acf, new DefaultAudioSink(o_abz, audioProcessorArr));
    }

    public aco(Context context, ahb o_ahb, @Nullable adf<adn> o_adf_o_adn, boolean z, @Nullable Handler handler, @Nullable acf o_acf, AudioSink audioSink) {
        super(1, o_ahb, o_adf_o_adn, z, 44100.0f);
        this.ˋ = context.getApplicationContext();
        this.ॱ = audioSink;
        this.ॱˋ = -9223372036854775807L;
        this.ˏ = new long[10];
        this.ˎ = new d(handler, o_acf);
        audioSink.ˎ(new b());
    }

    public int ˋ(ahb o_ahb, adf<adn> o_adf_o_adn, Format format) throws DecoderQueryException {
        String str = format.ʽ;
        if (!anb.ˏ(str)) {
            return 0;
        }
        int i = anr.ˋ >= 21 ? 32 : 0;
        boolean ˎ = aay.ˎ((adf) o_adf_o_adn, format.ʻ);
        if (ˎ && ˋ(format.ॱˎ, str) && o_ahb.ˋ() != null) {
            return (i | 8) | 4;
        }
        if (("audio/raw".equals(str) && !this.ॱ.ˎ(format.ॱˎ, format.ʿ)) || !this.ॱ.ˎ(format.ॱˎ, 2)) {
            return 1;
        }
        boolean z;
        int i2;
        DrmInitData drmInitData = format.ʻ;
        if (drmInitData != null) {
            z = false;
            for (i2 = 0; i2 < drmInitData.ˊ; i2++) {
                z |= drmInitData.ˎ(i2).ˏ;
            }
        } else {
            z = false;
        }
        List ˊ = o_ahb.ˊ(format.ʽ, z);
        if (ˊ.isEmpty()) {
            if (!z || o_ahb.ˊ(format.ʽ, false).isEmpty()) {
                i2 = 1;
            } else {
                i2 = 2;
            }
            return i2;
        } else if (!ˎ) {
            return 2;
        } else {
            ahe o_ahe = (ahe) ˊ.get(0);
            z = o_ahe.ˊ(format);
            int i3 = (z && o_ahe.ˋ(format)) ? 16 : 8;
            return (i | i3) | (z ? 4 : 3);
        }
    }

    public List<ahe> ˏ(ahb o_ahb, Format format, boolean z) throws DecoderQueryException {
        if (ˋ(format.ॱˎ, format.ʽ)) {
            ahe ˋ = o_ahb.ˋ();
            if (ˋ != null) {
                return Collections.singletonList(ˋ);
            }
        }
        return super.ˏ(o_ahb, format, z);
    }

    protected boolean ˋ(int i, String str) {
        return this.ॱ.ˎ(i, anb.ʻ(str));
    }

    public void ˊ(ahe o_ahe, MediaCodec mediaCodec, Format format, MediaCrypto mediaCrypto, float f) {
        this.ʼ = ˋ(o_ahe, format, ʻॱ());
        this.ᐝ = ˏ(o_ahe.ˎ);
        this.ॱॱ = ˊ(o_ahe.ˎ);
        this.ʽ = o_ahe.ʼ;
        MediaFormat ˏ = ˏ(format, o_ahe.ˊ == null ? "audio/raw" : o_ahe.ˊ, this.ʼ, f);
        mediaCodec.configure(ˏ, null, mediaCrypto, 0);
        if (this.ʽ) {
            this.ʻ = ˏ;
            this.ʻ.setString("mime", format.ʽ);
            return;
        }
        this.ʻ = null;
    }

    public int ॱ(MediaCodec mediaCodec, ahe o_ahe, Format format, Format format2) {
        if (ˊ(o_ahe, format2) <= this.ʼ && o_ahe.ॱ(format, format2, true) && format.ʽॱ == 0 && format.ˈ == 0 && format2.ʽॱ == 0 && format2.ˈ == 0) {
            return 1;
        }
        return 0;
    }

    public ana ˋ() {
        return this;
    }

    public float ˏ(float f, Format format, Format[] formatArr) {
        int i = -1;
        for (Format format2 : formatArr) {
            int i2 = format2.ʼॱ;
            if (i2 != -1) {
                i = Math.max(i, i2);
            }
        }
        return i == -1 ? SizeModifier.STABLE_STATE_SCALE : ((float) i) * f;
    }

    public void ˎ(String str, long j, long j2) {
        this.ˎ.ˊ(str, j, j2);
    }

    public void ˊ(Format format) throws ExoPlaybackException {
        super.ˊ(format);
        this.ˎ.ॱ(format);
        this.ˋॱ = "audio/raw".equals(format.ʽ) ? format.ʿ : 2;
        this.ॱˊ = format.ॱˎ;
        this.ͺ = format.ʽॱ;
        this.ˊॱ = format.ˈ;
    }

    public void ˊ(MediaCodec mediaCodec, MediaFormat mediaFormat) throws ExoPlaybackException {
        int ʻ;
        int[] iArr;
        if (this.ʻ != null) {
            ʻ = anb.ʻ(this.ʻ.getString("mime"));
            mediaFormat = this.ʻ;
        } else {
            ʻ = this.ˋॱ;
        }
        int integer = mediaFormat.getInteger("channel-count");
        int integer2 = mediaFormat.getInteger("sample-rate");
        if (this.ᐝ && integer == 6 && this.ॱˊ < 6) {
            iArr = new int[this.ॱˊ];
            for (int i = 0; i < this.ॱˊ; i++) {
                iArr[i] = i;
            }
        } else {
            iArr = null;
        }
        try {
            this.ॱ.ˎ(ʻ, integer, integer2, 0, iArr, this.ͺ, this.ˊॱ);
        } catch (Exception e) {
            throw ExoPlaybackException.ˊ(e, ॱᐝ());
        }
    }

    protected void ˋ(int i) {
    }

    protected void ʿ() {
    }

    protected void ॱ(int i, long j, long j2) {
    }

    public void ˏ(boolean z) throws ExoPlaybackException {
        super.ˏ(z);
        this.ˎ.ˎ(this.ˊ);
        int i = ॱˋ().ॱ;
        if (i != 0) {
            this.ॱ.ˋ(i);
        } else {
            this.ॱ.ॱॱ();
        }
    }

    public void ˏ(Format[] formatArr, long j) throws ExoPlaybackException {
        super.ˏ(formatArr, j);
        if (this.ॱˋ != -9223372036854775807L) {
            if (this.ॱᐝ == this.ˏ.length) {
                amz.ˏ("MediaCodecAudioRenderer", "Too many stream changes, so dropping change at " + this.ˏ[this.ॱᐝ - 1]);
            } else {
                this.ॱᐝ++;
            }
            this.ˏ[this.ॱᐝ - 1] = this.ॱˋ;
        }
    }

    public void ˏ(long j, boolean z) throws ExoPlaybackException {
        super.ˏ(j, z);
        this.ॱ.ᐝ();
        this.ˏॱ = j;
        this.ʻॱ = true;
        this.ᐝॱ = true;
        this.ॱˋ = -9223372036854775807L;
        this.ॱᐝ = 0;
    }

    public void ॱˊ() {
        super.ॱˊ();
        this.ॱ.ˏ();
    }

    public void ͺ() {
        ˎˎ();
        this.ॱ.ʽ();
        super.ͺ();
    }

    public void ॱˎ() {
        try {
            this.ॱˋ = -9223372036854775807L;
            this.ॱᐝ = 0;
            this.ॱ.ʼ();
            try {
                super.ॱˎ();
            } finally {
                this.ˊ.ॱ();
                this.ˎ.ˏ(this.ˊ);
            }
        } catch (Throwable th) {
            super.ॱˎ();
        } finally {
            this.ˊ.ॱ();
            this.ˎ.ˏ(this.ˊ);
        }
    }

    public boolean ʽॱ() {
        return super.ʽॱ() && this.ॱ.ˋ();
    }

    public boolean ˈ() {
        return this.ॱ.ˊ() || super.ˈ();
    }

    public long ॱ() {
        if (ˊ() == 2) {
            ˎˎ();
        }
        return this.ˏॱ;
    }

    public abq ॱ(abq o_abq) {
        return this.ॱ.ॱ(o_abq);
    }

    public abq e_() {
        return this.ॱ.ʻ();
    }

    public void ˊ(DecoderInputBuffer decoderInputBuffer) {
        if (this.ʻॱ && !decoderInputBuffer.i_()) {
            if (Math.abs(decoderInputBuffer.ˏ - this.ˏॱ) > 500000) {
                this.ˏॱ = decoderInputBuffer.ˏ;
            }
            this.ʻॱ = false;
        }
        this.ॱˋ = Math.max(decoderInputBuffer.ˏ, this.ॱˋ);
    }

    @CallSuper
    public void ॱ(long j) {
        while (this.ॱᐝ != 0 && j >= this.ˏ[0]) {
            this.ॱ.ॱ();
            this.ॱᐝ--;
            System.arraycopy(this.ˏ, 1, this.ˏ, 0, this.ॱᐝ);
        }
    }

    public boolean ˊ(long j, long j2, MediaCodec mediaCodec, ByteBuffer byteBuffer, int i, int i2, long j3, boolean z, Format format) throws ExoPlaybackException {
        Exception e;
        if (this.ॱॱ && j3 == 0 && (i2 & 4) != 0 && this.ॱˋ != -9223372036854775807L) {
            j3 = this.ॱˋ;
        }
        if (this.ʽ && (i2 & 2) != 0) {
            mediaCodec.releaseOutputBuffer(i, false);
            return true;
        } else if (z) {
            mediaCodec.releaseOutputBuffer(i, false);
            r0 = this.ˊ;
            r0.ʻ++;
            this.ॱ.ॱ();
            return true;
        } else {
            try {
                if (!this.ॱ.ॱ(byteBuffer, j3)) {
                    return false;
                }
                mediaCodec.releaseOutputBuffer(i, false);
                r0 = this.ˊ;
                r0.ˎ++;
                return true;
            } catch (InitializationException e2) {
                e = e2;
                throw ExoPlaybackException.ˊ(e, ॱᐝ());
            } catch (WriteException e3) {
                e = e3;
                throw ExoPlaybackException.ˊ(e, ॱᐝ());
            }
        }
    }

    public void ʾ() throws ExoPlaybackException {
        try {
            this.ॱ.ˎ();
        } catch (Exception e) {
            throw ExoPlaybackException.ˊ(e, ॱᐝ());
        }
    }

    public void ˎ(int i, @Nullable Object obj) throws ExoPlaybackException {
        switch (i) {
            case 2:
                this.ॱ.ˋ(((Float) obj).floatValue());
                return;
            case 3:
                this.ॱ.ˋ((aby) obj);
                return;
            case 5:
                this.ॱ.ˏ((acj) obj);
                return;
            default:
                super.ˎ(i, obj);
                return;
        }
    }

    protected int ˋ(ahe o_ahe, Format format, Format[] formatArr) {
        int ˊ = ˊ(o_ahe, format);
        if (formatArr.length != 1) {
            for (Format format2 : formatArr) {
                if (o_ahe.ॱ(format, format2, false)) {
                    ˊ = Math.max(ˊ, ˊ(o_ahe, format2));
                }
            }
        }
        return ˊ;
    }

    private int ˊ(ahe o_ahe, Format format) {
        if (anr.ˋ < 24 && "OMX.google.raw.decoder".equals(o_ahe.ˎ)) {
            Object obj = 1;
            if (anr.ˋ == 23) {
                PackageManager packageManager = this.ˋ.getPackageManager();
                if (packageManager != null && packageManager.hasSystemFeature("android.software.leanback")) {
                    obj = null;
                }
            }
            if (obj != null) {
                return -1;
            }
        }
        return format.ᐝ;
    }

    @SuppressLint({"InlinedApi"})
    protected MediaFormat ˏ(Format format, String str, int i, float f) {
        MediaFormat mediaFormat = new MediaFormat();
        mediaFormat.setString("mime", str);
        mediaFormat.setInteger("channel-count", format.ॱˎ);
        mediaFormat.setInteger("sample-rate", format.ʼॱ);
        ahc.ˊ(mediaFormat, format.ʼ);
        ahc.ˋ(mediaFormat, "max-input-size", i);
        if (anr.ˋ >= 23) {
            mediaFormat.setInteger("priority", 0);
            if (f != SizeModifier.STABLE_STATE_SCALE) {
                mediaFormat.setFloat("operating-rate", f);
            }
        }
        return mediaFormat;
    }

    private void ˎˎ() {
        long ˏ = this.ॱ.ˏ(ʽॱ());
        if (ˏ != Long.MIN_VALUE) {
            if (!this.ᐝॱ) {
                ˏ = Math.max(this.ˏॱ, ˏ);
            }
            this.ˏॱ = ˏ;
            this.ᐝॱ = false;
        }
    }

    private static boolean ˏ(String str) {
        return anr.ˋ < 24 && "OMX.SEC.aac.dec".equals(str) && "samsung".equals(anr.ˏ) && (anr.ˎ.startsWith("zeroflte") || anr.ˎ.startsWith("herolte") || anr.ˎ.startsWith("heroqlte"));
    }

    private static boolean ˊ(String str) {
        if (anr.ˋ < 21 && "OMX.SEC.mp3.dec".equals(str) && "samsung".equals(anr.ˏ) && (anr.ˎ.startsWith("baffin") || anr.ˎ.startsWith("grand") || anr.ˎ.startsWith("fortuna") || anr.ˎ.startsWith("gprimelte") || anr.ˎ.startsWith("j2y18lte") || anr.ˎ.startsWith("ms01"))) {
            return true;
        }
        return false;
    }
}
